package com.netcracker.lab1.utils;

import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * Created with IntelliJ IDEA.
 * User: nocturne
 * Date: 09.01.13
 * Time: 17:43
 * To change this template use File | Settings | File Templates.
 */

public class Generator {

    private static Generator instance = new Generator();

    private volatile Integer value = new Integer(0);
    private AtomicReferenceFieldUpdater<Generator, Integer> updater =
            AtomicReferenceFieldUpdater.newUpdater(Generator.class, Integer.class, "value");

    private Generator()
    {

    }

    public static Generator getInstance(){
        return instance;
    }

    public Integer next()
    {
        while(true)
        {
            Integer expected = value;
            Integer updated = 1;
            if(updater.compareAndSet(this, expected, updated))
                return updated;
        }
    }

}
